﻿Public Class frmResults
    Private mySet As FullSet
    Private lblItemNames(0 To 15) As Label
    Private lblMetaGem As Label
    Private lblGems(0 To 15, 0 To 2) As Label
    Private picSockets(0 To 15, 0 To 2) As PictureBox
    Private picMetaGem As PictureBox
    Private lblEnchants(0 To 15) As Label
    Private lblTotalStats As Label
    Private lblStatTitles(0 To 13) As Label
    Private lblStatValues(0 To 13) As Label
    Private numPieces As Integer

    ' Variables to determine how the labels are spaced.
    Private Const c_col1Top As Integer = 40
    Private Const c_col1Left As Integer = 12
    Private Const c_col2Top As Integer = 40
    Private Const c_col2Left As Integer = 362
    Private Const c_statsTop As Integer = 40
    Private Const c_statsLeft As Integer = 712

    Private Const c_nameBufferVert = 2

    Private Const c_picHoriz As Integer = 3
    Private Const c_picVert As Integer = 19
    Private Const c_picSize As Integer = 13

    Private Const c_itemPropHoriz As Integer = 22
    Private Const c_itemPropVert = 19

    Private Const c_itemSpaceVert As Integer = 20

    Private Const c_statsSectionWidth = 150

    Enum slotIdx As Integer
        head = 0
        shoulder
        chest
        hand
        leg
        neck
        back
        wrist
        waist
        foot
        finger1
        finger2
        trinket1
        trinket2
        weapon1
        weapon2
    End Enum

    Enum stats
        str
        ap
        crit
        haste
        arp
        agi
        hit
        exp
        armor
    End Enum

    Public Sub sendSet(ByVal inSet As FullSet)
        ' We don't need a new copy in memory here, so it's ok
        ' to do a reference copy just to bring it in scope.
        mySet = inSet

        Dim loopvar As Integer, loopvar2 As Integer

        numPieces = UBound(mySet.items) + 1

        ' Create all the label controls that we need.
        lblMetaGem = New Label
        Me.Controls.Add(lblMetaGem)
        lblMetaGem.Visible = False
        lblMetaGem.AutoSize = True
        Select Case mySet.metaGem
            Case GemSet.metaGems.csd
                lblMetaGem.Text = "Chaotic Skyflare Diamond"
            Case GemSet.metaGems.red
                lblMetaGem.Text = "Relentless Earthsiege Diamond"
        End Select
        picMetaGem = New PictureBox
        picMetaGem.Visible = False
        Me.Controls.Add(picMetaGem)
        picMetaGem.Width = 13
        picMetaGem.Height = 13
        picMetaGem.BackColor = Color.Gray
        For loopvar = 0 To numPieces - 1
            lblItemNames(loopvar) = New Label
            lblItemNames(loopvar).Visible = False
            Me.Controls.Add(lblItemNames(loopvar))
            lblItemNames(loopvar).AutoSize = True
            lblItemNames(loopvar).Font = New Font(lblItemNames(loopvar).Font, FontStyle.Bold)
            lblItemNames(loopvar).Text = mySet.items(loopvar).name
            If mySet.items(loopvar).name = "Shadowmourne" Then
                lblItemNames(loopvar).ForeColor = Color.Orange
            Else
                lblItemNames(loopvar).ForeColor = Color.Purple
            End If
            lblEnchants(loopvar) = New Label
            lblEnchants(loopvar).Visible = False
            Me.Controls.Add(lblEnchants(loopvar))
            lblEnchants(loopvar).AutoSize = True
            lblEnchants(loopvar).ForeColor = System.Drawing.Color.FromArgb(0, 192, 0)
            Select Case loopvar
                Case slotIdx.head
                    lblEnchants(loopvar).Text = enchString(mySet.headEnch)
                Case slotIdx.shoulder
                    lblEnchants(loopvar).Text = enchString(mySet.shoulderEnch)
                Case slotIdx.back
                    lblEnchants(loopvar).Text = enchString(mySet.backEnch)
                Case slotIdx.chest
                    lblEnchants(loopvar).Text = enchString(mySet.chestEnch)
                Case slotIdx.wrist
                    lblEnchants(loopvar).Text = enchString(mySet.wristEnch)
                Case slotIdx.hand
                    lblEnchants(loopvar).Text = enchString(mySet.handEnch)
                Case slotIdx.leg
                    lblEnchants(loopvar).Text = enchString(mySet.legEnch)
                Case slotIdx.foot
                    lblEnchants(loopvar).Text = enchString(mySet.footEnch)
                Case slotIdx.finger1
                    lblEnchants(loopvar).Text = enchString(mySet.finger1Ench)
                Case slotIdx.finger2
                    lblEnchants(loopvar).Text = enchString(mySet.finger2Ench)
                Case Else
                    lblEnchants(loopvar).Text = "None"
            End Select
            For loopvar2 = 0 To 2
                lblGems(loopvar, loopvar2) = New Label
                lblGems(loopvar, loopvar2).Visible = False
                Me.Controls.Add(lblGems(loopvar, loopvar2))
                picSockets(loopvar, loopvar2) = New PictureBox
                picSockets(loopvar, loopvar2).Visible = False
                Me.Controls.Add(picSockets(loopvar, loopvar2))
                lblGems(loopvar, loopvar2).AutoSize = True
                lblGems(loopvar, loopvar2).Text = mySet.gems(loopvar, loopvar2).name
                lblGems(loopvar, loopvar2).ForeColor = mySet.gems(loopvar, loopvar2).RGBColor
                picSockets(loopvar, loopvar2).BackColor = mySet.items(loopvar).RGBColor(loopvar2)
                picSockets(loopvar, loopvar2).Width = c_picSize
                picSockets(loopvar, loopvar2).Height = c_picSize
            Next
        Next

        ' Labels for total statistics area.
        lblTotalStats = New Label
        lblTotalStats.Font = New Font(lblTotalStats.Font, FontStyle.Bold)
        lblTotalStats.Text = "Total Stats"
        lblTotalStats.TextAlign = ContentAlignment.TopCenter
        lblTotalStats.AutoSize = False
        lblTotalStats.Width = c_statsSectionWidth
        lblTotalStats.Height = 17
        Me.Controls.Add(lblTotalStats)
        lblTotalStats.Visible = False

        For loopvar = 0 To 13
            lblStatTitles(loopvar) = New Label
            lblStatTitles(loopvar).AutoSize = True
            Me.Controls.Add(lblStatTitles(loopvar))
            lblStatTitles(loopvar).Visible = False
            lblStatValues(loopvar) = New Label
            lblStatValues(loopvar).AutoSize = True
            lblStatValues(loopvar).TextAlign = ContentAlignment.TopRight
            Me.Controls.Add(lblStatValues(loopvar))
            lblStatValues(loopvar).Visible = False
        Next

        lblStatTitles(0).Text = "Strength:"
        lblStatTitles(1).Text = "Attack Power:"
        lblStatTitles(2).Text = "Crit Rating:"
        lblStatTitles(3).Text = "Haste Rating:"
        lblStatTitles(4).Text = "ArP Rating:"
        lblStatTitles(5).Text = "Agility:"
        lblStatTitles(6).Text = "Armor:"
        lblStatTitles(7).Text = "Melee Hit %:"
        lblStatTitles(8).Text = "Spell Hit %:"
        lblStatTitles(9).Text = "Hit Rating:"
        lblStatTitles(10).Text = "Expertise:"
        lblStatTitles(11).Text = "Exp Rating:"
        lblStatTitles(12).Text = "Dodge Chance:"
        lblStatTitles(13).Text = "Total Score:"

        lblStatValues(0).Text = mySet.totalStats(stats.str)
        lblStatValues(1).Text = mySet.totalStats(stats.ap)
        lblStatValues(2).Text = mySet.totalStats(stats.crit)
        lblStatValues(3).Text = mySet.totalStats(stats.haste)
        lblStatValues(4).Text = mySet.totalStats(stats.arp)
        lblStatValues(5).Text = mySet.totalStats(stats.agi)
        lblStatValues(6).Text = mySet.totalStats(stats.armor)
        lblStatValues(7).Text = mySet.meleeHitPercent & "%"
        lblStatValues(8).Text = mySet.spellHitPercent & "%"
        lblStatValues(9).Text = mySet.totalStats(stats.hit)
        lblStatValues(10).Text = mySet.Expertise
        lblStatValues(11).Text = mySet.totalStats(stats.exp)
        lblStatValues(12).Text = System.Math.Round(mySet.dodgeChance, 2) & "%"
        lblStatValues(13).Text = System.Math.Round(mySet.totalScore, 2)

        ' Show the graphics as the starting point.
        Graphics_Click()
        optGraphical.Checked = True

    End Sub

    Private Sub optPlainText_CheckChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optPlainText.CheckedChanged
        If optPlainText.Checked = False Then
            Exit Sub
        End If

        ' Displays a plain text formatted string of the output including
        ' gems and enchants
        Dim txtString As String
        hideGraphics()
        txtTextFormats.Visible = True

        txtString = ""

        ' Stats First:
        txtString &= "Total Stats" & vbNewLine
        txtString &= "-----------" & vbNewLine
        txtString &= "Strength:     " & mySet.totalStats(stats.str) & vbNewLine
        txtString &= "Attack Power: " & mySet.totalStats(stats.ap) & vbNewLine
        txtString &= "Crit Rating:  " & mySet.totalStats(stats.crit) & vbNewLine
        txtString &= "Haste Rating: " & mySet.totalStats(stats.haste) & vbNewLine
        txtString &= "ArP Rating:   " & mySet.totalStats(stats.arp) & vbNewLine
        txtString &= "Agility:      " & mySet.totalStats(stats.agi) & vbNewLine
        txtString &= "Armor:        " & mySet.totalStats(stats.armor) & vbNewLine
        txtString &= vbNewLine
        txtString &= "Melee Hit %:  " & mySet.meleeHitPercent & "%" & vbNewLine
        txtString &= "Spell Hit %:  " & mySet.spellHitPercent & "%" & vbNewLine
        txtString &= "Hit Rating:   " & mySet.totalStats(stats.hit) & vbNewLine
        txtString &= "Expertise:    " & mySet.Expertise & vbNewLine
        txtString &= "Exp Rating:   " & mySet.totalStats(stats.exp) & vbNewLine
        txtString &= "Dodge Chance: " & System.Math.Round(mySet.dodgeChance, 2) & "%" & vbNewLine
        txtString &= vbNewLine
        txtString &= "Total Score:  " & System.Math.Round(mySet.totalScore, 2) & vbNewLine & vbNewLine

        txtString &= "Best Gear" & vbNewLine
        txtString &= "---------" & vbNewLine
        ' Example Item:
        '
        ' Koltira's Helmet of Triumph
        '   [Meta] Chaotic Skyflare Diamond
        '   [B] Bold Cardinal Ruby
        '   Arcanum of Torment
        Dim loopvar As Integer
        For loopvar = 0 To UBound(mySet.items)
            txtString &= PlainText(loopvar) & vbNewLine
        Next

        ' String is built, set the textbox.
        txtTextFormats.Text = txtString
    End Sub

    Private Function PlainText(ByVal slotIndex As slotIdx) As String
        Dim txtString As String = ""
        Dim loopvar As Integer
        ' return a plaintext of the given slotindex
        With mySet.items(slotIndex)
            txtString &= .name & vbNewLine
            If slotIndex = slotIdx.head Then
                ' Meta
                txtString &= "  [META] "
                Select Case mySet.metaGem
                    Case GemSet.metaGems.csd
                        txtString &= "Chaotic Skyflare Diamond"
                    Case GemSet.metaGems.red
                        txtString &= "Relentless Earthsiege Diamond"
                End Select
                txtString &= vbNewLine
            End If
            ' Other Sockets
            For loopvar = 0 To 2
                If .socketcolor(loopvar) <> Item.colors.none Then
                    Select Case .socketcolor(loopvar)
                        Case Item.colors.red
                            txtString &= "  [R] "
                        Case Item.colors.blue
                            txtString &= "  [B] "
                        Case Item.colors.yellow
                            txtString &= "  [Y] "
                        Case Item.colors.prismatic
                            txtString &= "  [P] "
                    End Select
                    txtString &= mySet.gems(slotIndex, loopvar).name & vbNewLine
                End If
            Next
            ' Enchants - do nothing if not enchantable slot.
            Select Case slotIndex
                Case slotIdx.head
                    txtString &= "  " & enchString(mySet.headEnch) & vbNewLine
                Case slotIdx.shoulder
                    txtString &= "  " & enchString(mySet.shoulderEnch) & vbNewLine
                Case slotIdx.chest
                    txtString &= "  " & enchString(mySet.chestEnch) & vbNewLine
                Case slotIdx.hand
                    txtString &= "  " & enchString(mySet.handEnch) & vbNewLine
                Case slotIdx.leg
                    txtString &= "  " & enchString(mySet.legEnch) & vbNewLine
                Case slotIdx.back
                    txtString &= "  " & enchString(mySet.backEnch) & vbNewLine
                Case slotIdx.wrist
                    txtString &= "  " & enchString(mySet.wristEnch) & vbNewLine
                Case slotIdx.foot
                    txtString &= "  " & enchString(mySet.footEnch) & vbNewLine
                Case slotIdx.finger1
                    If mySet.finger1Ench <> CalcManager.Enchants.none Then txtString &= "  " & enchString(mySet.finger1Ench) & vbNewLine
                Case slotIdx.finger2
                    If mySet.finger2Ench <> CalcManager.Enchants.none Then txtString &= "  " & enchString(mySet.finger2Ench) & vbNewLine
            End Select
        End With

        PlainText = txtString

    End Function

    Private Function enchString(ByVal inEnch As CalcManager.Enchants)
        ' Returns a string representing the name of the enchant passed in
        Select Case inEnch
            Case CalcManager.Enchants.back_GreaterSpeed
                enchString = "Enchant Cloak - Greater Speed"
            Case CalcManager.Enchants.back_MajorAgility
                enchString = "Enchant Cloak - Major Agility"
            Case CalcManager.Enchants.chest_PowerfulStats
                enchString = "Enchant Chest - Powerful Stats"
            Case CalcManager.Enchants.finger_Assault
                enchString = "Enchant Ring - Assault"
            Case CalcManager.Enchants.foot_GreaterAssault
                enchString = "Enchant Boots - Greater Assault"
            Case CalcManager.Enchants.foot_Icewalker
                enchString = "Enchant Boots - Icewalker"
            Case CalcManager.Enchants.foot_NitroBoosts
                enchString = "Nitro Boosts"
            Case CalcManager.Enchants.hand_Crusher
                enchString = "Enchant Gloves - Crusher"
            Case CalcManager.Enchants.hand_HyperspeedAccelerators
                enchString = "Hyperspeed Accelerators"
            Case CalcManager.Enchants.hand_MajorStrength
                enchString = "Enchant Gloves - Major Strength"
            Case CalcManager.Enchants.hand_Precision
                enchString = "Enchant Gloves - Precision"
            Case CalcManager.Enchants.head_ArcanumOfTorment
                enchString = "Arcanum of Torment"
            Case CalcManager.Enchants.leg_IcescaleArmor
                enchString = "Icescale Leg Armor"
            Case CalcManager.Enchants.shoulder_GreaterAxe
                enchString = "Greater Inscription of the Axe"
            Case CalcManager.Enchants.shoulder_MastersAxe
                enchString = "Master's Inscription of the Axe"
            Case CalcManager.Enchants.wrist_Expertise
                enchString = "Enchant Bracers - Expertise"
            Case CalcManager.Enchants.wrist_FurLining
                enchString = "Fur Lining - Attack Power"
            Case CalcManager.Enchants.wrist_GreaterAssault
                enchString = "Enchant Bracers - Greater Assault"
            Case Else
                enchString = "None"
        End Select
    End Function

    Private Sub optVBulletin_CheckChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optvBulletin.CheckedChanged
        If optvBulletin.Checked = False Then
            Exit Sub
        End If
        ' Makes a vBulletin Table of all of this for easy posting to EJ.  
        ' Possible incorporation of WoW item IDs later, but for now we'll just not itemlink the names of pieces.
        hideGraphics()
        txtTextFormats.Visible = True

        Dim txtString As String

        txtString = ""

        ' Stats First:
        txtString &= "[table][b]Total Stats[/b]|" & vbNewLine
        txtString &= "Strength:|" & mySet.totalStats(stats.str) & vbNewLine
        txtString &= "Attack Power:|" & mySet.totalStats(stats.ap) & vbNewLine
        txtString &= "Crit Rating:|" & mySet.totalStats(stats.crit) & vbNewLine
        txtString &= "Haste Rating:|" & mySet.totalStats(stats.haste) & vbNewLine
        txtString &= "ArP Rating:|" & mySet.totalStats(stats.arp) & vbNewLine
        txtString &= "Agility:|" & mySet.totalStats(stats.agi) & vbNewLine
        txtString &= "Armor:|" & mySet.totalStats(stats.armor) & vbNewLine
        txtString &= "|" & vbNewLine
        txtString &= "Melee Hit %:|" & mySet.meleeHitPercent & "%" & vbNewLine
        txtString &= "Spell Hit %:|" & mySet.spellHitPercent & "%" & vbNewLine
        txtString &= "Hit Rating:|" & mySet.totalStats(stats.hit) & vbNewLine
        txtString &= "Expertise:|" & mySet.Expertise & vbNewLine
        txtString &= "Exp Rating:|" & mySet.totalStats(stats.exp) & vbNewLine
        txtString &= "Dodge Chance:|" & System.Math.Round(mySet.dodgeChance, 2) & "%" & vbNewLine
        txtString &= "|" & vbNewLine
        txtString &= "Total Score:|" & System.Math.Round(mySet.totalScore, 2)
        txtString &= "[/table]" & vbNewLine & vbNewLine

        txtString &= "[table=""head""][b]Item Name[/b]|{colsp=3}[b]Gems[/b]|[b]Enchants[/b]" & vbNewLine

        ' Example Item:
        '
        ' Koltira's Helmet of Triumph | [Chaotic Skyflare Diamond][Bold Cardinal Ruby]|[Arcanum of Torment]
        Dim loopvar As Integer
        For loopvar = 0 To UBound(mySet.items)
            txtString &= vBulletin(loopvar)
            If loopvar <> UBound(mySet.items) Then
                txtString &= vbNewLine
            End If
        Next
        txtString &= "[/table]" & vbNewLine

        ' String is built, set the textbox.
        txtTextFormats.Text = txtString
    End Sub

    Private Function vBulletin(ByVal slotIndex As slotIdx) As String
        ' Returns a row of a vBulletin Table
        Dim txtString As String = ""
        Dim loopvar As Integer
        ' return a plaintext of the given slotindex
        With mySet.items(slotIndex)
            If Double.Parse(.wowheadID) > 0 Then
                txtString &= "[url=http://www.wowhead.com/?item=" & .wowheadID & "][b]"
                If .name <> "Shadowmourne" Then
                    txtString &= "[color=""#b048f8""]"
                Else
                    txtString &= "[color=""#ff8000""]"
                End If
                txtString &= "[" & .name & "]"
                txtString &= "[/color][/b][/url]"
            Else
                txtString &= .name
            End If
            txtString &= "|"
            If slotIndex = slotIdx.head Then
                ' Meta
                txtString &= "[url=http://www.wowhead.com/?item="
                Select Case mySet.metaGem
                    Case GemSet.metaGems.csd
                        txtString &= "41285][b][color=""#0080ff""][Chaotic Skyflare"
                    Case GemSet.metaGems.red
                        txtString &= "41398][b][color=""#0080ff""][Relentless Earthsiege"
                End Select
                txtString &= "][/color][/b][/url]"
                txtString &= "|"
            End If
            ' Other Sockets
            For loopvar = 0 To 2
                If .socketcolor(loopvar) <> Item.colors.none Then
                    Select Case .socketcolor(loopvar)
                        Case Item.colors.red
                            txtString &= "[item]"
                        Case Item.colors.blue
                            txtString &= "[item]"
                        Case Item.colors.yellow
                            txtString &= "[item]"
                        Case Item.colors.prismatic
                            txtString &= "[item]"
                    End Select
                    txtString &= mySet.gems(slotIndex, loopvar).name
                    txtString &= "[/item]"
                End If
                If Not (loopvar = 2 And slotIndex = slotIdx.head) Then
                    txtString &= "|"
                End If
            Next
                ' Enchants - do nothing if not enchantable slot.
            Select Case slotIndex
                Case slotIdx.head
                    txtString &= "[item]"
                    Select Case enchString(mySet.headEnch)
                        Case "Arcanum of Torment"
                            txtString &= "44149"
                    End Select
                    txtString &= "[/item]"
                Case slotIdx.shoulder
                    txtString &= "[item]"
                    Select Case enchString(mySet.shoulderEnch)
                        Case "Greater Inscription of the Axe"
                            txtString &= "44133"
                        Case "Master's Inscription of the Axe"
                            txtString &= "61117"
                    End Select
                    txtString &= "[/item]"
                Case slotIdx.chest
                    txtString &= "[url=http://www.wowhead.com/?spell="
                    Select Case enchString(mySet.chestEnch)
                        Case "Enchant Chest - Powerful Stats"
                            txtString &= "60692][b][color=""#1eff00""][Enchant Chest - Powerful Stats"
                    End Select
                    txtString &= "][/color][/b][/url]"
                Case slotIdx.hand
                    txtString &= "[url=http://www.wowhead.com/?spell="
                    Select Case enchString(mySet.handEnch)
                        Case "Enchant Gloves - Greater Assault"
                            txtString &= "44513][b][color=""#1eff00""][Enchant Gloves - Greater Assault"
                        Case "Enchant Gloves - Precision"
                            txtString &= "44488][b][color=""#1eff00""][Enchant Gloves - Precision"
                        Case "Enchant Gloves - Major Strength"
                            txtString &= "33995][b][color=""#1eff00""][Enchant Gloves - Major Strength"
                        Case "Hyperspeed Accelerators"
                            txtString &= "54999][b][color=""#1eff00""][Hyperspeed Accelerators"
                    End Select
                    txtString &= "][/color][/b][/url]"
                Case slotIdx.leg
                    txtString &= "[item]"
                    Select Case enchString(mySet.legEnch)
                        Case "Icescale Leg Armor"
                            txtString &= "38374"
                    End Select
                    txtString &= "[/item]"
                Case slotIdx.back
                    txtString &= "[url=http://www.wowhead.com/?spell="
                    Select Case enchString(mySet.backEnch)
                        Case "Enchant Cloak - Major Agility"
                            txtString &= "60663][b][color=""#1eff00""][Enchant Cloak - Major Agility"
                        Case "Enchant Cloak - Greater Speed"
                            txtString &= "47898][b][color=""#1eff00""][Enchant Cloak - Greater Speed"
                        Case "Swordguard Embroidery"
                            txtString &= "55777][b][color=""#1eff00""][Swordguard Embroidery"
                    End Select
                    txtString &= "][/color][/b][/url]"
                Case slotIdx.wrist
                    txtString &= "[url=http://www.wowhead.com/?spell="
                    Select Case enchString(mySet.wristEnch)
                        Case "Enchant Bracers - Greater Assault"
                            txtString &= "44575][b][color=""#1eff00""][Enchant Bracers - Greater Assault"
                        Case "Enchant Bracers - Expertise"
                            txtString &= "44598][b][color=""#1eff00""][Enchant Bracers - Expertise"
                        Case "Fur Lining - Attack Power"
                            txtString &= "57683][b][color=""#1eff00""][Fur Lining - Attack Power"
                    End Select
                    txtString &= "][/color][/b][/url]"
                Case slotIdx.foot
                    txtString &= "[url=http://www.wowhead.com/?spell="
                    Select Case enchString(mySet.footEnch)
                        Case "Enchant Boots - Icewalker"
                            txtString &= "60623][b][color=""#1eff00""][Enchant Boots - Icewalker"
                        Case "Enchant Boots - Greater Assault"
                            txtString &= "60763][b][color=""#1eff00""][Enchant Boots - Greater Assault"
                        Case "Nitro Boosts"
                            txtString &= "55016][b][color=""#1eff00""][Nitro Boosts"
                    End Select
                    txtString &= "][/color][/b][/url]"
                Case slotIdx.finger1
                    If mySet.finger1Ench <> CalcManager.Enchants.none Then
                        txtString &= "[url=http://www.wowhead.com/?spell=44645][b][color=""#1eff00""][Enchant Ring - Assault]][/color][/b][/url]"
                    Else
                        txtString &= "None"
                    End If
                Case slotIdx.finger2
                    If mySet.finger2Ench <> CalcManager.Enchants.none Then
                        txtString &= "[url=http://www.wowhead.com/?spell=44645][b][color=""#1eff00""][Enchant Ring - Assault]][/color][/b][/url]"
                    Else
                        txtString &= "None"
                    End If
                Case Else
                    txtString &= "None"
            End Select
        End With

        vBulletin = txtString
    End Function

    Private Sub optXML_CheckChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optXML.CheckedChanged
        If optXML.Checked = False Then
            Exit Sub
        End If
        ' Makes an XML representation of the results set.  Not sure what this'll
        ' be used for yet, but who knows.
        hideGraphics()
        txtTextFormats.Visible = True
        Dim txtString As String

        txtString = ""

        ' Stats First:
        txtString &= "<?xml version=""1.0"" encoding=""utf-8""?>" & vbNewLine
        txtString &= "<results>" & vbNewLine
        txtString &= vbTab & "<stats>" & vbNewLine
        txtString &= vbTab & vbTab & "<str>" & mySet.totalStats(stats.str) & "</str>" & vbNewLine
        txtString &= vbTab & vbTab & "<ap>" & mySet.totalStats(stats.ap) & "</ap>" & vbNewLine
        txtString &= vbTab & vbTab & "<crit>" & mySet.totalStats(stats.crit) & "</crit>" & vbNewLine
        txtString &= vbTab & vbTab & "<haste>" & mySet.totalStats(stats.haste) & "</haste>" & vbNewLine
        txtString &= vbTab & vbTab & "<arp>" & mySet.totalStats(stats.arp) & "</arp>" & vbNewLine
        txtString &= vbTab & vbTab & "<agi>" & mySet.totalStats(stats.agi) & "</agi>" & vbNewLine
        txtString &= vbTab & vbTab & "<armor>" & mySet.totalStats(stats.armor) & "</armor>" & vbNewLine
        txtString &= vbTab & vbTab & "<meleehitpercent>" & mySet.meleeHitPercent & "%" & "</meleehitpercent>" & vbNewLine
        txtString &= vbTab & vbTab & "<spellhitpercent>" & mySet.spellHitPercent & "%" & "</spellhitpercent>" & vbNewLine
        txtString &= vbTab & vbTab & "<hitrating>" & mySet.totalStats(stats.hit) & "</hitrating>" & vbNewLine
        txtString &= vbTab & vbTab & "<expertise>" & mySet.Expertise & "</expertise>" & vbNewLine
        txtString &= vbTab & vbTab & "<exprating>" & mySet.totalStats(stats.exp) & "</exprating>" & vbNewLine
        txtString &= vbTab & vbTab & "<dodgechance>" & System.Math.Round(mySet.dodgeChance, 2) & "%" & "</dodgechance>" & vbNewLine
        txtString &= vbTab & vbTab & "<totalscore>" & System.Math.Round(mySet.totalScore, 2) & "</totalscore>" & vbNewLine
        txtString &= vbTab & "</stats>" & vbNewLine

        ' Items
        txtString &= vbTab & "<items>" & vbNewLine

        ' Example Item:
        '
        ' <item name="Koltira's Triumphant Elephant">
        '      <metagem>Chaotic Skyflare Diamond</metagem>
        '      <socket0>Bold CardinalRuby</socket0>
        '      <socket1>None</socket1>
        '      <socket2>None</socket2
        '      <enchant>Arcanum of Torment</enchant>
        ' </item>
        Dim loopvar As Integer
        For loopvar = 0 To UBound(mySet.items)
            txtString &= toXML(loopvar)
            txtString &= vbNewLine
        Next
        txtString &= vbTab & "</items>" & vbNewLine
        txtString &= "</results>"

        ' String is built, set the textbox.
        txtTextFormats.Text = txtString
    End Sub

    Private Function toXML(ByVal slotIndex As slotIdx) As String
        ' Returns an XML record for the item
        Dim txtString As String = ""
        Dim loopvar As Integer
        ' return a plaintext of the given slotindex
        With mySet.items(slotIndex)
            txtString &= vbTab & vbTab & "<item name=""" & .name & """>" & vbNewLine
            txtString &= vbTab & vbTab & vbTab & "<metagem>"
            If slotIndex = slotIdx.head Then
                ' Meta
                Select Case mySet.metaGem
                    Case GemSet.metaGems.csd
                        txtString &= "Chaotic Skyflare Diamond"
                    Case GemSet.metaGems.red
                        txtString &= "Relentless Earthsiege Diamond"
                End Select
            Else
                txtString &= "None"
            End If
            txtString &= "</metagem>" & vbNewLine
            ' Other Sockets
            For loopvar = 0 To 2
                If .socketcolor(loopvar) <> Item.colors.none Then
                    txtString &= vbTab & vbTab & vbTab & "<socket" & loopvar & ">"
                    txtString &= mySet.gems(slotIndex, loopvar).name
                    txtString &= "</socket" & loopvar & ">" & vbNewLine
                Else
                    txtString &= vbTab & vbTab & vbTab & "<socket" & loopvar & ">None</socket" & loopvar & ">" & vbNewLine
                End If
            Next
            txtString &= vbTab & vbTab & vbTab & "<enchant>"
            ' Enchants - do nothing if not enchantable slot.
            Select Case slotIndex
                Case slotIdx.head
                    txtString &= enchString(mySet.headEnch)
                Case slotIdx.shoulder
                    txtString &= enchString(mySet.shoulderEnch)
                Case slotIdx.chest
                    txtString &= enchString(mySet.chestEnch)
                Case slotIdx.hand
                    txtString &= enchString(mySet.handEnch)
                Case slotIdx.leg
                    txtString &= enchString(mySet.legEnch)
                Case slotIdx.back
                    txtString &= enchString(mySet.backEnch)
                Case slotIdx.wrist
                    txtString &= enchString(mySet.wristEnch)
                Case slotIdx.foot
                    txtString &= enchString(mySet.footEnch)
                Case slotIdx.finger1
                    If mySet.finger1Ench <> CalcManager.Enchants.none Then
                        txtString &= enchString(mySet.finger1Ench)
                    Else
                        txtString &= "None"
                    End If
                Case slotIdx.finger2
                    If mySet.finger2Ench <> CalcManager.Enchants.none Then
                        txtString &= enchString(mySet.finger2Ench)
                    Else
                        txtString &= "None"
                    End If

                Case Else
                    txtString &= "None"
            End Select
            txtString &= "</enchant>" & vbNewLine
            txtString &= vbTab & vbTab & "</item>"
        End With

        toXML = txtString
    End Function

    Private Sub Graphics_Click()
        ' Shows the set in fancy graphical style.  This is the default results format
        ' since it is the easiest to view and comprehend immediately.
        txtTextFormats.Visible = False
        Dim curLeftTop As Integer = c_col1Top
        Dim curRightTop As Integer = c_col2Top

        ' Ok, everything has the values that it needs.  Stats haven't been done yet, obviously.
        curLeftTop = placeGraphics(slotIdx.head, c_col1Left, curLeftTop)
        curRightTop = placeGraphics(slotIdx.hand, c_col2Left, curRightTop)
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        curLeftTop = placeGraphics(slotIdx.neck, c_col1Left, curLeftTop)
        curRightTop = placeGraphics(slotIdx.waist, c_col2Left, curRightTop)
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        curLeftTop = placeGraphics(slotIdx.shoulder, c_col1Left, curLeftTop)
        curRightTop = placeGraphics(slotIdx.leg, c_col2Left, curRightTop)
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        curLeftTop = placeGraphics(slotIdx.back, c_col1Left, curLeftTop)
        curRightTop = placeGraphics(slotIdx.foot, c_col2Left, curRightTop)
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        curLeftTop = placeGraphics(slotIdx.chest, c_col1Left, curLeftTop)
        curRightTop = placeGraphics(slotIdx.finger1, c_col2Left, curRightTop)
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        curRightTop = placeGraphics(slotIdx.finger2, c_col2Left, curRightTop)
        curRightTop = placeGraphics(slotIdx.trinket1, c_col2Left, curRightTop)

        curLeftTop = curRightTop
        curLeftTop = placeGraphics(slotIdx.wrist, c_col1Left, curLeftTop)
        curRightTop = placeGraphics(slotIdx.trinket2, c_col2Left, curRightTop)
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        curLeftTop = placeGraphics(slotIdx.weapon1, c_col1Left, curLeftTop)
        If numPieces = 16 Then
            curRightTop = placeGraphics(slotIdx.weapon2, c_col2Left, curRightTop)
        End If
        If curLeftTop > curRightTop Then
            curRightTop = curLeftTop
        Else
            curLeftTop = curRightTop
        End If

        Me.Height = curLeftTop + (c_itemSpaceVert * 2)

        ' Set the stats labels.
        lblTotalStats.Left = c_statsLeft
        lblTotalStats.Top = c_statsTop
        lblTotalStats.Visible = True

        For loopvar = 0 To 13
            lblStatTitles(loopvar).Left = c_statsLeft
            lblStatTitles(loopvar).Top = c_statsTop + c_itemPropVert + c_nameBufferVert + (loopvar * c_itemPropVert)
            lblStatTitles(loopvar).Visible = True
            lblStatValues(loopvar).Left = c_statsLeft + c_statsSectionWidth - lblStatValues(loopvar).Width
            lblStatValues(loopvar).Top = c_statsTop + c_itemPropVert + c_nameBufferVert + (loopvar * c_itemPropVert)
            lblStatValues(loopvar).Visible = True
        Next

        Me.Width = c_statsLeft + c_statsSectionWidth + c_itemPropHoriz
        Me.MinimumSize = New System.Drawing.Size(Me.Width, Me.Height)

    End Sub

    Private Function placeGraphics(ByVal slotIndex As slotIdx, ByVal Left As Integer, ByVal Top As Integer) As Integer
        ' Returns the bottom of the graphics once placed.
        Dim curPropNum As Integer = 1
        Dim loopvar As Integer
        Dim lastBottom As Integer = 0

        lblItemNames(slotIndex).Left = Left
        lblItemNames(slotIndex).Top = Top
        lblItemNames(slotIndex).Visible = True
        lastBottom = lblItemNames(slotIndex).Top + lblItemNames(slotIndex).Height + c_nameBufferVert
        If slotIndex = slotIdx.head Then
            ' Meta gem
            lblMetaGem.Left = Left + c_itemPropHoriz
            lblMetaGem.Top = Top + c_itemPropVert + c_nameBufferVert
            picMetaGem.Left = Left + c_picHoriz
            picMetaGem.Top = Top + c_picVert + c_nameBufferVert
            lblMetaGem.Visible = True
            picMetaGem.Visible = True
            curPropNum += 1
            lastBottom = lblMetaGem.Top + lblMetaGem.Height
        End If
        ' Gem sockets
        For loopvar = 0 To 2
            If mySet.items(slotIndex).socketcolor(loopvar) <> Item.colors.none Then
                ' Place the image and the label.
                lblGems(slotIndex, loopvar).Left = Left + c_itemPropHoriz
                lblGems(slotIndex, loopvar).Top = Top + c_nameBufferVert + (curPropNum * c_itemPropVert)
                picSockets(slotIndex, loopvar).Left = Left + c_picHoriz
                picSockets(slotIndex, loopvar).Top = Top + c_nameBufferVert + (curPropNum * c_picVert)
                lblGems(slotIndex, loopvar).Visible = True
                picSockets(slotIndex, loopvar).Visible = True
                curPropNum += 1
                lastBottom = lblGems(slotIndex, loopvar).Top + lblGems(slotIndex, loopvar).Height
            Else
                lblGems(slotIndex, loopvar).Visible = False
                picSockets(slotIndex, loopvar).Visible = False
            End If
        Next
        ' Enchant
        If lblEnchants(slotIndex).Text <> "None" Then
            lblEnchants(slotIndex).Left = Left + c_itemPropHoriz
            lblEnchants(slotIndex).Top = Top + c_nameBufferVert + (curPropNum * c_itemPropVert)
            lblEnchants(slotIndex).Visible = True
            lastBottom = lblEnchants(slotIndex).Top + lblEnchants(slotIndex).Height
        Else
            lblEnchants(slotIndex).Visible = False
        End If

        placeGraphics = lastBottom + c_itemSpaceVert

    End Function

    Private Sub hideGraphics()
        ' Make everything invisible.
        lblMetaGem.Visible = False
        picMetaGem.Visible = False
        lblTotalStats.Visible = False

        Dim loopvar As Integer, loopvar2 As Integer

        For loopvar = 0 To numPieces - 1
            lblItemNames(loopvar).Visible = False
            lblEnchants(loopvar).Visible = False
            For loopvar2 = 0 To 2
                lblGems(loopvar, loopvar2).Visible = False
                picSockets(loopvar, loopvar2).Visible = False
            Next
        Next

        For loopvar = 0 To 13
            lblStatTitles(loopvar).Visible = False
            lblStatValues(loopvar).Visible = False
        Next

    End Sub

    Private Sub btnFinished_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinished.Click
        ' Done looking
        Me.Close()
        Me.Dispose()
    End Sub

    Private Sub optGraphical_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optGraphical.CheckedChanged
        If optGraphical.Checked = True Then
            Graphics_Click()
        End If
    End Sub
End Class